/*
 * @Author: your name
 * @Date: 2021-05-19 09:23:10
 * @LastEditTime: 2021-05-25 15:22:45
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vite-project/src/router/menus/index.ts
 */
import type { Menu, MenuModule } from '@/router/types';
import type { RouteRecordNormalized } from 'vue-router';
import { transformMenuModule } from '@/router/helper/menuHelper';
import { usePermissionStore } from '@/store/modules/permission';
import projectSetting from '@/settings/projectSetting';
import { PermissionModeEnum } from '@/enums/appEnum';
import router from '@/router';

const modules = import.meta.globEager('./modules/**/*.ts');

const menuModules: MenuModule[] = [];

// 将menus下的菜单文件整合
Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  menuModules.push(...modList);
});

// 静态资源菜单
const staticMenus: Menu[] = [];
(() => {
  menuModules.sort((a, b) => {
    return (a.orderNo || 0) - (b.orderNo || 0);
  });

  for (const menu of menuModules) {
    staticMenus.push(transformMenuModule(menu));
  }
})();

const getAsyncMenus = (): Menu[] => {
  const permissionStore = usePermissionStore();
  if (projectSetting.permissionMode == PermissionModeEnum.ROLE) {
    return staticMenus;
  } else {
    return permissionStore.getMenuList;
  }
}

export const getMenus = (): Menu[] => {

  const menus = getAsyncMenus();
  const routes = router.getRoutes();

  return menus;
}